<template>
  <span><component @getTabBarDatep="TabBarDate" :startTime="startTime" :startDateType = 'startDateType' :is="spinner"></component></span>
</template>
<script>
  import spinnerall from './datetabbar/all';
  import spinneryear from './datetabbar/year';
  import spinnermonth from './datetabbar/month';
  import spinnerdate from './datetabbar/day';
  import spinnerYM from './datetabbar/yearMonth';
  /**
   * datetabber
   * @module common/plugins/datebabbarp
   * @param {sstringlot} [getTabBarDate] - 父组件写一个方法  参数即为获取的时间
   * @param {type} [all,year,month,date,ym]  根据不同type调取相应组件状态，all是可选择周日年的模式，其他type，根据语义得知该模式
   * @author zhangdongliang
   * @example
   * <template>
   * <datetabbar type = 'all' :startTime="1456654756554" :startDateType = '1'  @getTabBarDate = "TabBarDate"></datetabbar>
   * </template>
   * TabBarDate: (date) => {
   *    console.log(date);
   *   }
   */
  const SPINNERS = [
    'all',
    'year',
    'month',
    'date',
    'ym'
  ];
  const parseSpinner = function (index) {
    if ({}.toString.call(index) === '[object Number]') {
      if (SPINNERS.length <= index) {
        index = 0;
      }
      return SPINNERS[index];
    }
    if (SPINNERS.indexOf(index) === -1) {
      index = SPINNERS[0];
    }
    return index;
  };

  export default {
    name: 'mt-spinner',
    data () {
      return {
        date: 0
      };
    },
    computed: {
      spinner () {
        return `${parseSpinner(this.type)}`;
      }
    },
    components: {
      all: spinnerall,
      year: spinneryear,
      month: spinnermonth,
      date: spinnerdate,
      ym: spinnerYM
    },
    watch: {
      date: function () {
        this.$emit('getTabBarDate', this.date);
      }
    },
    methods: {
      TabBarDate: function (date) {
        this.date = date;
      }
    },
    props: {
      type: {
        default: 0
      },
      startTime: {
        default () {
          let num = Date.parse(new Date());
          return num;
        }
      },
      startDateType: {
      }
    }
  };
</script>
